/**
 * @fileOverview diagram node
 * @author huangtonger@aliyun.com
 */

const Diagram = require('../../diagram');
const Util = require('../../util/');
const Global = require('../../global');

Diagram.registerNode('diagram-base', {
  getSize(item) {
    const model = item.getModel();
    return Util.getNodeSize(model.size);
  },
  defaultFillPalette: 0,
  defaultStrokePalette: 3,
  activedFillPalette: 0,
  activedStrokePalette: 5,
  selectedFillPalette: 2,
  selectedStrokePalette: 5,
  getDefaulStyle() {
    return Global.nodeStyle;
  },
  getDefaulActivedStyle() {
    return Global.nodeActivedStyle;
  },
  getDefaulSelectedtyle() {
    return Global.nodeSelectedStyle;
  },
  getStyle(item) {
    const model = item.getModel();
    const color = model.color;
    let fill;
    let stroke;

    if (color) {
      const palettes = Util.Palettes.generate(color);
      fill = palettes[this.defaultFillPalette];
      stroke = palettes[this.defaultStrokePalette];
    }
    return Util.mix(true, {}, this.getDefaulStyle(), {
      fill,
      stroke
    }, model.style);
  },
  getPath(item) {
    const size = this.getSize(item);
    const style = this.getStyle(item);
    return Util.getRectPath(-size[0] / 2, -size[1] / 2, size[0], size[1], style.radius);
  },
  getActivedOutterStyle() {
    return Global.nodeActivedOutterStyle;
  },
  getActivedStyle(item) {
    const model = item.getModel();
    const defaultActivedStyle = this.getDefaulActivedStyle(item);
    const { color } = model;
    if (color) {
      const palettes = Util.Palettes.generate(color);
      return {
        ...defaultActivedStyle,
        fill: palettes[this.activedFillPalette],
        stroke: palettes[this.activedStrokePalette]
      };
    }
    return defaultActivedStyle;
  },
  getSelectedStyle(item) {
    const model = item.getModel();
    const defaultSelectedtyle = this.getDefaulSelectedtyle(item);
    const { color } = model;
    if (color) {
      const palettes = Util.Palettes.generate(color);
      return {
        ...defaultSelectedtyle,
        fill: palettes[this.selectedFillPalette],
        stroke: palettes[this.selectedStrokePalette]
      };
    }
    return defaultSelectedtyle;
  },
  getSelectedOutterStyle(item) {
    const model = item.getModel();
    const { color } = model;
    if (color) {
      const palettes = Util.Palettes.generate(color);
      return {
        ...Global.nodeSelectedOutterStyle,
        stroke: palettes[1],
        fill: palettes[1]
      };
    }
    return Global.nodeSelectedOutterStyle;
  },
  anchor: [
    [ 0.5, 0 ], // 上面边的中点
    [ 1, 0.5 ], // 右边边的中点
    [ 0.5, 1 ], // 下边边的中点
    [ 0, 0.5 ] // 左边边的中点
  ]
});

Diagram.registerNode('capsule', {
  getPath(item) {
    const size = this.getSize(item);
    return Util.getRectPath(-size[0] / 2, -size[1] / 2, size[0], size[1], size[1] / 2);
  }
});

Diagram.registerNode('circle', {
  getPath(item) {
    const size = this.getSize(item);
    const width = size[0];
    const height = size[1];
    return Util.getEllipsePath(0, 0, width / 2, height / 2);
  }
});

Diagram.registerNode('rhombus', {
  getPath(item) {
    const size = this.getSize(item);
    const x = 0;
    const y = 0;
    const width = size[0];
    const height = size[1];
    const points = [{
      x,
      y: y - height / 2
    }, {
      x: x + width / 2,
      y
    }, {
      x,
      y: y + height / 2
    }, {
      x: x - width / 2,
      y
    }, {
      x,
      y: y - height / 2
    }];
    return Util.pointsToPolygon(points);
  }
});
